package org.example.cdpcrpbackend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.example.cdpcrpbackend.controller.dto.Class1;
import org.example.cdpcrpbackend.controller.dto.StudentClassAdmin;
import org.example.cdpcrpbackend.domain.Class;
import org.example.cdpcrpbackend.domain.Student;
import org.example.cdpcrpbackend.mapper.ClassMapper;
import org.example.cdpcrpbackend.mapper.StudentMapper;
import org.example.cdpcrpbackend.service.ClassService;
import org.example.cdpcrpbackend.util.MyType;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {
    private final ClassMapper classMapper;
    private final StudentMapper studentMapper;

    @Override
    public StudentClassAdmin login(String username, String password) {
        // 当前学号和班级表进行多表查询
        Student student = classMapper.login(username, password);
        if (student == null) {
            return null;
        }

        // 通过学号查询学生详情（不需要，上面通过 login 查询出来了）
        // 通过班级id查询班级详情
        Class classInfo = classMapper.selectById(student.getClassId());
        StudentClassAdmin info = new StudentClassAdmin();

        // 清除密码（重要）
        student.setPassword("");
        info.setStudent(student);
        info.setClassInfo(classInfo);
        info.setRole(MyType.RoleClass);
        return info;
    }

    @Override
    public List<Class1> listByPage(String key, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return classMapper.listByCondition(key);
    }

    @Override
    public Class1 getClassInfoById(String id) {
        return classMapper.getClassInfoById(id);
    }


}
